package kr.hkit.powerjava.chapter23;

import java.nio.Buffer;

class Producer implements Runnable {
	private Buffer buffer;

	public Producer(Buffer buffer) {
		this.buffer = buffer;
	}

	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
			buffer.put();
			System.out.println("생산자 : " + i + "번 케익을 생산하였습니다.");
			try {
				Thread.sleep((int) (Math.random() * 100));
			} catch (InterruptedException e) {

			}

		}
	}
}

class Consumer implements Runnable{
	private Buffer buffer;
	
	public Consumer(Buffer drop) {
		this.buffer = drop;
	}

	@Override
	public void run() {
		for (int i = 0; i < 10; i++) {
			int data=buffer.get();
			System.out.println("소비자 : "+data+"번 케익을 소비하였습니다.");
			try{
				Thread.sleep((int)(Math.random()*100));
			}catch (InterruptedException e){
				
			}
		}		
	}
	
}

public class ProducerCinsumerTest {
	public static void main(String[] args) {
		Buffer buffer=new Buffer();
		(new Thread(new Producer(buffer))).start();
		(new Thread(new Consumer(buffer))).start();
	}

}
